package win.devhu.testing.SpringBoot2Kotlin.restful

import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.RestController
import java.util.*

/**
 * restful jpa
 * */

@RestController
@RequestMapping("/employee")
class RestController(val employeeRepository: EmployeeRepository)
{
    @PostMapping
    fun saveEmployee(@RequestBody employee: Employee): Employee
    {
        return employeeRepository.save(employee)
    }

    @GetMapping("/{id}")
    fun getEmployee(@PathVariable id: Long): Optional<Employee>
    {
        return employeeRepository.findById(id)
    }

    @GetMapping
    fun getAllEmployees(): Iterable<Employee>
    {
        return employeeRepository.findAll()
    }

    @PutMapping
    fun updateEmployee(@RequestBody employee: Employee)
    {
        employeeRepository.save(employee)
    }

    @DeleteMapping("/{id}")
    fun deleteEmployee(@PathVariable id: Long)
    {
        employeeRepository.deleteById(id)
    }
}